package cn.com.css.taxInspection.util;

import lombok.Data;

import java.util.Collections;
import java.util.List;

public class PageUtil {

    public static <T> PageResult<T> paginate(List<T> fullList, int pageNum, int pageSize) {
        if (fullList == null || fullList.isEmpty()) {
            return new PageResult<>(0, Collections.emptyList());
        }

        int total = fullList.size();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);

        if (fromIndex >= total || fromIndex < 0) {
            return new PageResult<>(total, Collections.emptyList());
        }

        List<T> pagedList = fullList.subList(fromIndex, toIndex);
        return new PageResult<>(total, pagedList);
    }

    @Data
    public static class PageResult<T> {
        private int total;
        private List<T> records;

        public PageResult(int total, List<T> records) {
            this.total = total;
            this.records = records;
        }

    }
}